package com.hil.okhtttp.interceptor;

import android.util.Log;

import com.hil.okhtttp.OkHttpClient2;
import com.hil.okhtttp.RealCall2;
import com.hil.okhtttp.Response2;

import java.io.IOException;
// 重试拦截器
public class ReRequestInterceptor implements Interceptor2{
    String TAG = ReRequestInterceptor.class.getSimpleName();
    @Override
    public Response2 doNext(Chain2 chain2) throws IOException {
        Log.d(TAG, "我是重试拦截器，执行了");
        ChainManager chainManager = (ChainManager) chain2;
        RealCall2 call2 = chainManager.getCall();
        OkHttpClient2 client2 = call2.getOkHttpClient2();
        IOException ioException = null;
// 重试次数
        if (client2.getRecount() != 0) {
            for (int i = 0; i < client2.getRecount(); i++) { // 3
                try {
                    Log.d(TAG, "我是重试拦截器，我要Return Response2了");
                    Response2 response2 = chain2.getResponse(chainManager.getRequest());// 执行下一个拦截器（任务节点）
                    return response2;
                } catch (IOException e) {
                    ioException = e;
                }

            }
        }

        throw ioException;
    }

}
